package com.nervenets.shopping.hibernate.domain.order;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.adapter.jsonType.StringArrayJson;
import com.nervenets.general.exception.LogicException;
import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.general.utils.SpringContextHolder;
import com.nervenets.general.utils.StringUtils;
import com.nervenets.shopping.enumeration.OrderStatus;
import com.nervenets.shopping.enumeration.PayType;
import com.nervenets.shopping.hibernate.domain.shopping.Product;
import com.nervenets.shopping.hibernate.domain.user.User;
import com.nervenets.shopping.hibernate.domain.user.vo.UserVo;
import com.nervenets.shopping.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class GroupBuyOrderUser extends DomainObject {
    public final static String TRADE_NO_PREFIX = "GO";
    @ApiModelProperty(value = "商品")
    @ManyToOne
    @JoinColumn(columnDefinition = "product_id")
    @JsonIgnore
    @JSONField(serialize = false)
    private Product product;
    @ApiModelProperty(value = "订单状态")
    @Column(columnDefinition = "tinyint")
    @Builder.Default
    private OrderStatus status = OrderStatus.init;
    @ApiModelProperty(value = "支付金额")
    private int money;
    @ApiModelProperty(value = "抵扣额度")
    private int subsidy;
    @ApiModelProperty(value = "订单奖励")
    private int reward;
    @ApiModelProperty(value = "收货人名称")
    private String name;
    @ApiModelProperty(value = "收货人电话")
    private String mobile;
    @ApiModelProperty(value = "收货人地址")
    private String address;
    @ApiModelProperty(value = "发货时间")
    private long expressTime;
    @ApiModelProperty(value = "运单公司")
    private String expressName;
    @ApiModelProperty(value = "运单单号")
    private String expressNumber;
    @ApiModelProperty(value = "支付成功时间")
    private long payTime;
    @ApiModelProperty(value = "成团处理时间")
    private long groupedTime;
    @ApiModelProperty(value = "退款成功时间")
    private long refundTime;
    @ApiModelProperty(value = "奖励成功时间")
    private long rewardTime;
    @ApiModelProperty(value = "支付方式")
    @Column(columnDefinition = "tinyint")
    private PayType payType;
    @ApiModelProperty("三方订单号")
    private String outTradeNo;
    @ApiModelProperty("日志")
    @Type(type = StringArrayJson.NAME)
    @Column(columnDefinition = "json")
    private List<String> logs = new ArrayList<>();
    @ApiModelProperty("是否通知")
    private boolean notify = false;
    @ApiModelProperty("是否中奖")
    private boolean bingo = false;
    @ApiModelProperty("备注")
    private String remark;

    @ApiModelProperty("第三方支付信息")
    @Transient
    private Object thirdPayInfo;

    public UserVo getCreator() {
        if (getCreateBy() > 0) {
            UserService userService = SpringContextHolder.getBean(UserService.class);
            User user = userService.fastFind(getCreateBy());
            if (null != user) {
                UserVo vo = new UserVo(user);
                if (user.getParentId() > 0) {
                    User inviteUser = userService.fastFind(user.getParentId());
                    if (null != inviteUser) vo.setInviteUser(new UserVo(inviteUser));
                }
                return vo;
            }
        }
        return null;
    }

    public long getProductId() {
        return this.product.getId();
    }

    public String getProductName() {
        return this.product.getName();
    }

    public String getTradeNo() {
        if (isNew()) throw new LogicException("order no saved.");
        return TRADE_NO_PREFIX + StringUtils.randomString(5).toUpperCase() + getId();
    }

    public void addLog(String log) {
        if (null == this.logs) this.logs = new ArrayList<>();
        this.logs.add(String.format("%s,%s", JodaUtils.timeLongToString(), log));
    }
}
